package com.lyy.msgtool.controller.vo;

import io.swagger.annotations.ApiModelProperty;

import java.beans.Transient;
import java.io.Serializable;
import java.util.function.Function;

/**
 * @ClassName BaseResult
 * @Description TODO
 * @Author louyangyi
 * @Date 2022/1/17 11:26
 **/
public class BaseResult<T> implements Serializable {
    private static final long serialVersionUID = 6803323956728517039L;
    private static final String SUCCESS_CODE = "0";
    @ApiModelProperty(
            required = true,
            value = "错误码",
            dataType = "String",
            example = "0",
            position = 1
    )
    private String code;
    @ApiModelProperty(
            required = true,
            value = "返回描述信息",
            dataType = "String",
            example = "SUCCESS",
            position = 2
    )
    private String msg;
    @ApiModelProperty(
            value = "返回数据封装",
            position = 3
    )
    private T data;

    public BaseResult() {
    }

    public BaseResult(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public BaseResult(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public static <T> BaseResult<T> success(T data) {
        return new BaseResult("0", "SUCCESS", data);
    }

    public static <S, R> BaseResult<R> success(S data, Function<S, R> mapper) {
        return success(mapper.apply(data));
    }

    @Transient
    public boolean isSuccess() {
        return "0".equals(this.code);
    }

    public String getCode() {
        return this.code;
    }

    public String getMsg() {
        return this.msg;
    }

    public T getData() {
        return this.data;
    }

    public void setCode(final String code) {
        this.code = code;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof BaseResult)) {
            return false;
        } else {
            BaseResult<?> other = (BaseResult)o;
            if (!other.canEqual(this)) {
                return false;
            } else {
                String msg;
                String otherMsg;
                label42: {
                    msg = this.getCode();
                    otherMsg = other.getCode();
                    if (msg == null) {
                        if (otherMsg == null) {
                            break label42;
                        }
                    } else if (msg.equals(otherMsg)) {
                        break label42;
                    }

                    return false;
                }

                msg = this.getMsg();
                otherMsg = other.getMsg();
                if (msg == null) {
                    if (otherMsg != null) {
                        return false;
                    }
                } else if (!msg.equals(otherMsg)) {
                    return false;
                }

                Object data = this.getData();
                Object otherData = other.getData();
                if (data == null) {
                    if (otherData != null) {
                        return false;
                    }
                } else if (!data.equals(otherData)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof BaseResult;
    }


    public String toString() {
        return "BaseResult(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ")";
    }
}
